import requests
import json
#直接刷新页面获取设置cookie
# url = "http://106.52.182.140/fanwe"
#
# re = requests.get(url)
# # print(re.headers)
# cookie = re.headers['Set-Cookie'].split(";",1)
# print("请求前的cookie值：{}".format(cookie[0]))

#登录方维
url_login ='http://106.52.182.140/fanwe/index.php?ctl=user&act=dologin&fhash=DPXquAioRACYJTGOxOZbJRyQKIjTkWBHwCHsmXfQXNhuzqklUY'
# header_login = {
#     "Host":"106.52.182.140",
#     "X-Requested-With":"XMLHttpRequest",
#     "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
#     "Origin":"http://106.52.182.140",
#     "Referer":"http://106.52.182.140/fanwe/index.php?ctl=user&act=login"
#     # "Cookie":cookie[0]
# }
data_login = {
    "ajax":1,
    "email":"cxytest004",
    "user_pwd":"UUZVYXV0QUFicUdpRGlUYlZRREpBdnlrSmFrSElCdW5qTUlycFRFT0dybEtQellzYnclMjV1NjVCOSUyNXU3RUY0Y3h5MTIzNDU2JTI1dThGNkYlMjV1NEVGNg=="
}

re = requests.post(url=url_login,data=data_login)
print(re.content.decode("unicode_escape"))
cookie = re.headers["Set-Cookie"].split(';',1)[0]
#re.headers ---响应头信息（是一个字典）
print(re.cookies)   #打印响应头cookie信息
print("登录后的cookie值：{}".format(re.headers["Set-Cookie"]))
print(type(cookie))
# PHPSESSID = cookie.spilt("=",1)[1]
# print(PHPSESSID)
#修改用户数据
url_2 = "http://106.52.182.140/fanwe/member.php?ctl=uc_account&act=save"
# header = {
#     "Host":"106.52.182.140",
#     "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
#     "Origin":"http://106.52.182.140",
#     "Referer":"http://106.52.182.140/fanwe/member.php?ctl=uc_account",
#     "Cookie":cookie
# }

cookies = re.cookies
data_2 = {
    "avatar_file":"",
    "graduation":"研究生或以上",
    "university":"kfaksjdk",
    "marriage":"未婚",
    "province_id":"14",
    "city_id":"197",
    "address":"长沙芙蓉区",
    "phone":"",
    "commit":"保存更改"
}

re_2 = requests.post(url=url_2,cookies=cookies,data=data_2)

print("请求头信息为：{}".format(re_2.request.headers)) #请求头
print(re_2.headers) #是打印响应头信息
print(re_2.text)
# print(type(re_2.text))